package gew.beans;

import gew.data.DimensionValue;
import gew.data.TradeTopNPartnersData;
import gew.data.TradeTopNPartnersDataRow;
import gew.metadata.CustomSql;
import gew.metadata.Queries;
import gew.services.DimensionValueService;
import gew.system.Labels;
import gew.system.RequestParameter;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean(name = "trdBnTopN")
@RequestScoped
public class TradeBeanChartTopNPartners {
    
    private static final int OTHER_PARTNERS_ID = 999;

    @EJB
    private DataFacade2 df;
    
    @EJB
    private DimensionValueService dvs;

    private RequestParameter rp;

    private String reporter;
    private String tradeFlowFA;
    private String tradeFlow;
    private String commodityCode;
    private String commodityName;
    
    private List<DimensionValue> partners;
    
    TradeTopNPartnersData data;

    @PostConstruct
    public void init() {
        rp = new RequestParameter((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(), dvs);
        Integer reporterId = rp.getReporterIds().size() > 0 ? rp.getReporterIds().get(0) : 0;
        reporter = dvs.getNameById(Queries.CountryDimensionValue, reporterId, rp.getLocale());
        Integer tradeFlowId = rp.getTradeFlowIds().size() > 0 ? rp.getTradeFlowIds().get(0) : 0;
        tradeFlowFA = dvs.getFontAwesomeById(Queries.TradeFlowDimensionValue, tradeFlowId, rp.getLocale());
        tradeFlow = dvs.getNameById(Queries.TradeFlowDimensionValue, tradeFlowId, rp.getLocale());
        Integer commodityId = rp.getCommodityIds().size() > 0 ? rp.getCommodityIds().get(0) : 0;
        commodityCode = dvs.getCodeById(Queries.CommodityDimensionValue, commodityId, rp.getLocale());
        commodityName = dvs.getNameById(Queries.CommodityDimensionValue, commodityId, rp.getLocale());
        
        String periodIds = rp.getPeriodIdsCS();
        Integer tradeValuePartnerTotalRank = rp.getPartnerRank();
        Integer partnerTotalId = 194;//dvs.getParentId(Queries.CountryDimensionValue, rp.getLocale(), reporterId);

        String q1 = CustomSql.getTradeTopNPartners2Data(rp.getLocaleUrlCode(), reporterId, tradeFlowId, periodIds, commodityId, tradeValuePartnerTotalRank, partnerTotalId, OTHER_PARTNERS_ID);
        data = df.getTradeTopNPartnersData(q1);
        partners = dvs.getSelectedValues(data.getColIds(), rp.getLocale(), Queries.CountryDimensionValue);
        
    }

    public String getLocaleUrlCode() {
        return rp.getLocaleUrlCode();
    }

    public String getReporter() {
        return reporter;
    }

    public String getTradeFlowFA() {
        return tradeFlowFA;
    }

    public String getTradeFlow() {
        return tradeFlow;
    }

    public String getUI_TOP_PARTNERS() {
        return Labels.get_UI_TOP_PARTNERS(rp.getLocale());
    }

    public String getCommodityCode() {
        return commodityCode;
    }

    public String getCommodityName() {
        return commodityName;
    }

    public Iterable<TradeTopNPartnersDataRow> getRows() {
        return data.getRows();
    }

    public Iterable<DimensionValue> getColumns() {
        return partners;
    }

    public String getUI_TITLE_PERIOD() {
        return Labels.get_UI_TITLE_PERIOD(rp.getLocale());
    }
    
    public String getUI_TITLE_REPORTERS() {
        return Labels.get_UI_TITLE_REPORTER(rp.getLocale());
    }

    public String getUI_TITLE_TRADE_FLOW() {
        return Labels.get_UI_TITLE_TRADE_FLOW(rp.getLocale());
    }

    public String getUI_TITLE_PARTNER() {
        return Labels.get_UI_TITLE_PARTNER(rp.getLocale());
    }

    public String getUI_TITLE_COMMODITY_CLASSIFICATION() {
        return Labels.get_UI_TITLE_COMMODITY_CLASSIFICATION(rp.getLocale());
    }

    public String getUI_TITLE_COMMODITY() {
        return Labels.get_UI_TITLE_COMMODITY(rp.getLocale());
    }

    public String getUI_TITLE_TRADE_VALUE() {
        return Labels.get_UI_TITLE_TRADE_VALUE(rp.getLocale());
    }

    public String getUI_TITLE_TRADE_SHARE_TOTAL() {
        return Labels.get_UI_TITLE_TRADE_SHARE_TOTAL(rp.getLocale());
    }

    public String getUI_VALUE_UOM_USD() {
        return Labels.get_UI_VALUE_UOM_USD(rp.getLocale());
    }
    
}
